package com.lanou3g.k12api.web.controller;

import com.lanou3g.k12api.entity.ArticleCategory;
import com.lanou3g.k12api.utils.K12Result;
import com.lanou3g.k12api.utils.K12ResultUtils;
import com.lanou3g.k12api.utils.RandomEntityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Api(description = "资讯分类管理")
@RestController
public class ArticleCategoryController {

    @ApiOperation(value = "新增资讯分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "资讯分类ID（新增接口不可传ID）", paramType = "form"),
            @ApiImplicitParam(name = "name", value = "资讯分类名称", required = true, example = "教育", paramType = "form"),
            @ApiImplicitParam(name = "remark", value = "备注", example = "这是一条测试资讯分类", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "状态（0 代表正常，1 代表停用）", required = true, defaultValue = "0",
                    allowableValues = "0,1", paramType = "form")
    })
    @PostMapping("/article/category")
    public K12Result create(ArticleCategory articleCategory) {
        System.out.println(articleCategory);
        return K12ResultUtils.ok();
    }

    @ApiOperation(value = "通过ID删除资讯分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要删除的ID", example = "1")
    })
    @DeleteMapping("/article/category/{id}")
    public K12Result delete(@PathVariable Long id) {
        System.out.println(id);
        return K12ResultUtils.ok();
    }

    @ApiOperation(value = "通过ID批量删除资讯分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "要删除的ID（逗号分隔）", example = "1,2,3")
    })
    @DeleteMapping("/article/categories/{ids}")
    public K12Result delete(@PathVariable Long[] ids) {
        System.out.println(Arrays.toString(ids));
        return K12ResultUtils.ok();
    }

    @ApiOperation(value = "通过ID修改资讯分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "资讯分类ID", required = true, example = "1"),
            @ApiImplicitParam(name = "name", value = "资讯分类名称", required = true, example = "教育", paramType = "form"),
            @ApiImplicitParam(name = "remark", value = "备注", example = "这是一条测试资讯分类", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "状态（0 代表正常，1 代表停用）", required = true, defaultValue = "0",
                    allowableValues = "0,1", paramType = "form")
    })
    @PutMapping("/article/category/{id}")
    public K12Result update(@PathVariable Long id, ArticleCategory articleCategory) {
        System.out.println(id);
        System.out.println(articleCategory.getId());
        return K12ResultUtils.ok();
    }

    @ApiOperation(value = "通过ID获取资讯分类", notes = "status：0 代表正常，1 代表停用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要获取的资讯分类的ID", example = "1")
    })
    @GetMapping("/article/category/{id}")
    public K12Result findById(@PathVariable Long id) {
        ArticleCategory articleCategory = RandomEntityUtils.random(ArticleCategory.class);
        articleCategory.setId(id);
        articleCategory.setStatus(Math.random() >= 0.1 ? 0 : 1);
        return K12ResultUtils.ok(articleCategory);
    }

    @ApiOperation(value = "获取资讯分类列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页数据量（建议不传）", defaultValue = "30"),
            @ApiImplicitParam(name = "keyword", value = "查询关键词"),
    })
    @GetMapping("/article/categories")
    public K12Result list(@RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "30") int size,
                          @RequestParam(defaultValue = "") String keyword) {
        System.out.println(page + "\t" + size + "\t" + keyword);
        List<ArticleCategory> articleCategories = RandomEntityUtils.random(ArticleCategory.class, size);
        return K12ResultUtils.ok(articleCategories);
    }
}
